#include "NetworkNode.h"

#include "RakNetworkFactory.h"

#include <cstdio>

NetworkNode::NetworkNode() : m_Peer(RakNetworkFactory::GetRakPeerInterface()) {
}

NetworkNode::~NetworkNode() {
	RakNetworkFactory::DestroyRakPeerInterface(m_Peer);
}

void NetworkNode::handleNetwork() {
	m_packet = m_Peer->Receive();
	if (m_packet) {
		switch (m_packet->data[0]) {
		case ID_REMOTE_DISCONNECTION_NOTIFICATION:
			m_packetHandler->handleRemoteDisconnectNotification(m_packet);
			break;
		case ID_REMOTE_CONNECTION_LOST:
			m_packetHandler->handleRemoteConnectionLost(m_packet);
			break;
		case ID_REMOTE_NEW_INCOMING_CONNECTION:
			m_packetHandler->handleRemoteIncomingConnection(m_packet);
			break;
		case ID_CONNECTION_REQUEST_ACCEPTED:
			m_packetHandler->handleConnectionAccepted(m_packet);
			break;
		case ID_NEW_INCOMING_CONNECTION:
			m_packetHandler->handleIncomingConnection(m_packet);
			break;
		case ID_NO_FREE_INCOMING_CONNECTIONS:
			m_packetHandler->handleNoIncomingConnections(m_packet);
			break;
		case ID_DISCONNECTION_NOTIFICATION:
			m_packetHandler->handleDisconnectNotification(m_packet);
			break;
		case ID_CONNECTION_LOST:
			m_packetHandler->handleConnectionLost(m_packet);
			break;
		case ID_CONNECTED_PONG:
			m_packetHandler->handlePong(m_packet);
			break;
		case ID_ALREADY_CONNECTED:
			printf("Lol, already connected.\n");
			break;
		case ID_CONNECTION_ATTEMPT_FAILED:
			printf("Connection attempt failed.\n");
			break;
		default:
			printf("Message with identifier %i has arrived.\n", m_packet->data[0]);
			break;
		}
	}
	m_Peer->DeallocatePacket(m_packet);
}